﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class CT_PhuCapBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public bool KiemTraPhuCap(int thang, int nam, int manv, int mapc)
        {
            if (DB.CT_PHUCAPs.Any(a => a.Thang == thang && a.Nam == nam && a.MaNV == manv && a.MaPhuCap == mapc))
                return true;
            else
                return false;
        }

        public List<CTPHUCAP> LayDSPCThang(int thang, int nam)
        {
            List<CTPHUCAP> pc = (from a in DB.CT_PHUCAPs
                                 where a.Thang == thang && a.Nam == nam
                                 select new CTPHUCAP
                                 {
                                     MaNV = a.MaNV,
                                     MaPhuCap = a.MaPhuCap
                                 }).ToList();
            return pc;
        }

        public List<int> LayPCThang(int manv, int thang, int nam)
        {
            List<int> mapc = (from a in DB.CT_PHUCAPs
                              where a.MaNV == manv && a.Thang == thang && a.Nam == nam
                              select a.MaPhuCap).ToList();
            return mapc;
        }

        public List<int> LayTienPCThang(int manv, int thang, int nam)
        {
            List<int> mapc = (from a in DB.CT_PHUCAPs
                              where a.MaNV == manv && a.Thang == thang && a.Nam == nam
                              select (int)a.PHUCAP.TienPhuCap).ToList();
            return mapc;
        }

        public void Them(CT_PHUCAP ctpc)
        {
            DB.CT_PHUCAPs.InsertOnSubmit(ctpc);
            DB.SubmitChanges();
        }

        public void XoaPC(int manv, int thang, int nam)
        {
            List<int> mapc = LayPCThang(manv, thang, nam);

            foreach(var i in mapc) 
            {
                CT_PHUCAP ctpc = DB.CT_PHUCAPs.Single(a => a.MaNV == manv && a.Thang == thang && a.Nam == nam && a.MaPhuCap == i);
                DB.CT_PHUCAPs.DeleteOnSubmit(ctpc);
            }
        }
    }
}
